package cn.skycity.common.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import cn.skycity.common.service.ISysMenuService;
import cn.skycity.framework.core.model.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.skycity.common.log.annotation.Log;
import com.skycity.common.log.enums.BusinessType;
import com.skycity.framework.uaa.pojo.entity.SysMenu;
import com.skycity.framework.uaa.pojo.vo.MenuTreeSelectVo;
import com.skycity.framework.uaa.pojo.vo.SysMenuVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {
    @Resource
    private ISysMenuService menuService;


    @GetMapping("/list")
    public Result<List<SysMenu>> getMenuList(SysMenu sysMenu) {
        return Result.success(menuService.list(new LambdaQueryWrapper<SysMenu>()
                .eq(StrUtil.isNotEmpty(sysMenu.getVisible()),SysMenu::getVisible,sysMenu.getVisible())
                .eq(StrUtil.isNotEmpty(sysMenu.getStatus()),SysMenu::getStatus,sysMenu.getStatus())
                .like(StrUtil.isNotEmpty(sysMenu.getMenuName()),SysMenu::getMenuName,sysMenu.getMenuName())
                .orderByAsc(SysMenu::getSort)
        ));
    }

    //
//    @ApiOperation(value = "菜单详情")
//    @ApiImplicitParam(name = "id", value = "菜单id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result<SysMenu> detail(@PathVariable Long id) {
        SysMenu menu = menuService.getById(id);
        return Result.success(menu);
    }

    //    @ApiOperation(value = "新增菜单")
////    @CacheEvict(cacheNames = "system",key = "'routeList'")
    @PostMapping("")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    public Result<?> addMenu(@RequestBody SysMenu sysMenu) {
        if ("0".equals(sysMenu.getMenuType())) {
            sysMenu.setComponent("Layout");
        }
        return Result.success(menuService.saveMenu(sysMenu));
    }

//    @ApiOperation(value = "修改菜单")
    @PutMapping
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
////    @CacheEvict(cacheNames = "system",key = "'routeList'")
    public Result<String> update(@RequestBody SysMenu sysMenu) {
        boolean result = menuService.updateMenu(sysMenu);
        return Result.judge(result);
    }

    //    @ApiOperation(value = "删除菜单")
//    @ApiImplicitParam(name = "ids", value = "id集合字符串，以,分割", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("/{ids}")
    @Transactional
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
//    @CacheEvict(cacheNames = "system",key = "'routeList'")
    public Result delete(@PathVariable("ids") Long id) {
        boolean result = menuService.removeById(id);
        return Result.judge(result);
    }


//    @SaCheckPermission("system:menu:query")
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public Result<MenuTreeSelectVo> roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        List<SysMenuVo> menus = menuService.selectMenuList();
        MenuTreeSelectVo selectVo = new MenuTreeSelectVo();
        selectVo.setCheckedKeys(menuService.selectMenuListByRoleId(roleId));
        selectVo.setMenus(menuService.buildMenuTreeSelect(menus));
        return Result.success(selectVo);
    }

    @GetMapping(value = "/treeselect")
    public Result<List<Tree<Long>>> treeselect() {
        List<SysMenuVo> menus = menuService.selectMenuList();
        return Result.success(menuService.buildMenuTreeSelect(menus));
    }
}
